#-- vim:sw=2:et
#++
#
# :title: Simple stock lookup plugin for RBot and Yahoo
# :author: Jason Hines, <jason@greenhell.com>
# :version: 0.1
#

# YAHOO returns CSV
# http://download.finance.yahoo.com/d/quotes?s=gnw&fields=snl1c1ohgvd1t1
# "GNW","GENWORTH FINANCIA",9.66,-0.92,10.06,10.49,9.58,2292900,"9/26/2008","11:54am"
#
# "s"  => "Symbol",
# "n"  => "Name",
# "l1" => "Last",
# "c1" => "Change",
# "o"  => "Opening",
# "h"  => "High",
# "g"  => "Low",
# "v"  => "Volume",
# "d1" => "Date",
# "t1" => "Time"
#

class StockPlugin < Plugin

  def stock(m, params)
    symbol = params[:symbol].to_s
    resp = @bot.httputil.get("http://download.finance.yahoo.com/d/quotes?s=#{symbol}&fields=snl1c1ohgvd1t1")
    data = resp.split(',')
    if data[3]=="N/A"
      m.reply "#{data[0]} Currently unavailable"
    else
      symbol = data[0].gsub(/\"/,'')
      name = data[1].gsub(/\"/,'')
      last = data[2]
      change = data[3]
      opening = data[4]
      high = data[5]
      low = data[6]
      vol = data[7].to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1,")
      date = data[8].gsub(/\"/,'')
      time = data[9].gsub(/\"/,'').chomp

      m.reply "#{symbol}: #{name} (Last: #{last}, Vol: #{vol}) [#{date} #{time}]"
      m.reply "-> Day Change: #{change}, Opened: #{opening} (Low: #{low} / High: #{high})" unless opening=="N/A"
    end
  end

  def help(plugin, topic="")
    "stock <symbol> => lookup stock information about <symbol>"
  end

end

plugin = StockPlugin.new

plugin.map "stock :symbol"

